package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.OperateCode;
import com.hunttown.mes.manage.controller.common.AdminBaseClass;
import com.hunttown.mes.manage.controller.common.ModelService;
import com.hunttown.mes.manage.service.TransportOperateLogManageService;
import com.hunttown.mes.manage.service.AnalysisWebsiteParamManageService;
import com.hunttown.mes.manage.service.AnalysisManageManageService;
import com.hunttown.mes.rpc.domain.AnalysisManageDTO;
import com.hunttown.mes.rpc.domain.AnalysisWebsiteParamDTO;
import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.IpUtils;
import com.hunttown.mes.common.utils.JSONUtils;
import com.hunttown.mes.common.utils.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @title: 站点配置
 * @author: wangjunfu
 * @date: 2020-08-18 22:29:17
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/website")
public class WebsiteParamController {

    private final static Logger logger = LoggerFactory.getLogger(WebsiteParamController.class);

    private final AnalysisWebsiteParamManageService websiteParamService;
    private final AnalysisManageManageService manageService;
    private final TransportOperateLogManageService logService;
    private final ModelService modelService;

    @Autowired
    public WebsiteParamController(AnalysisWebsiteParamManageService websiteParamService, AnalysisManageManageService manageService, TransportOperateLogManageService logService, ModelService modelService) {
        this.websiteParamService = websiteParamService;
        this.manageService = manageService;
        this.logService = logService;
        this.modelService = modelService;
    }

    //region 编辑&保存

    /**
     * 编辑
     *
     * @param model model模型
     * @param id    主键id
     * @return vm
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, HttpServletResponse response, Model model, Integer id) {
        if (id == null || id < 0) {
            id = 0;
        }

        AnalysisWebsiteParamDTO obj = null;

        //获取用户企业ID，然后根据企业ID去获取数据
        String pin = AdminBaseClass.getPin(request);
        Integer entId = modelService.getCurrentEnterprise(response, pin);

        if (entId > 0) {
            obj = websiteParamService.getByEntId(entId);
        }

        if (obj == null) {
            obj = new AnalysisWebsiteParamDTO();
            obj.setId(0);
            obj.setLastUpdateTime(new Date());
            obj = websiteParamService.insert(obj);
        }
        model.addAttribute("Obj", obj);

        return "basic/website/edit";
    }

    /**
     * 保存 ajax异步
     *
     * @param request sevelet请求
     * @param objDTO  实体类
     * @return json
     */
    @RequestMapping(value = "/saveEdit", method = RequestMethod.POST)
    @ResponseBody
    public String saveEditNotify(HttpServletRequest request, AnalysisWebsiteParamDTO objDTO) {

        // 结果返回 结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();

        try {
            if (objDTO.getIsLoginVerify() == null) {
                objDTO.setIsLoginVerify(0);
            }
            if (objDTO.getIsLoginQuestion() == null) {
                objDTO.setIsLoginQuestion(0);
            }

            String pin = AdminBaseClass.getPin(request);
            AnalysisManageDTO curr_obj = manageService.getFromCachePin(pin);
            objDTO.setLastUpdatePeople(curr_obj.getNickName());
            objDTO.setLastUpdateTime(new Date());

            if (objDTO.getId() == null || objDTO.getId() <= 0) {
                //新增
                //设置初始值
                objDTO = websiteParamService.insert(objDTO);

                if (objDTO == null || objDTO.getId() <= 0) {
                    //操作失败
                    map = OperateCode.CompositionResult(map, -1);
                    result = JSONUtils.toJSON(map);
                    return result;
                }

            } else {
                //更新
                //将不需要更新的字段设置为NULL
                Boolean isNext = websiteParamService.updateInfoById(objDTO);

                if (!isNext) {
                    //操作失败
                    map = OperateCode.CompositionResult(map, -1);
                    result = JSONUtils.toJSON(map);
                    return result;
                }
            }

            //region *** 更新信息完毕记录操作日志 ***
            logService.insertSimple(1, 36, objDTO.getId(), 2,
                    "更新网站信息",
                    FastJsonUtils.toJSONString(objDTO),
                    curr_obj.getId(),
                    IpUtils.getClientIp(request),
                    SystemUtils.getBrowserInfo(request));
            //endregion

            //操作成功 结果返回
            map = OperateCode.CompositionResult(map, 1);
            result = JSONUtils.toJSON(map);
            return result;

        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }
    //endregion

}